package com.yanqu.road.dao.impl.channelcenter.luckydraw;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.luckydraw.LuckyDrawPoolData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LuckyDrawPoolDataDaoImpl extends TempDataObjectDao<LuckyDrawPoolData> {

    public static String getLuckyDrawPoolDataSql = "select * from `t_n_lucky_draw_pool` where `activityId`=?";

    public LuckyDrawPoolData getLuckyDrawPoolData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getChannelServerDb());
            call = getDb().getConnection().prepareCall(getLuckyDrawPoolDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateLuckyDrawPoolDataSql = "update `t_n_lucky_draw_pool` set drawTimesMap=?,todayRealRewardTimes=?,resetTime=? where activityId=?";

    @Override
    protected boolean updateData(LuckyDrawPoolData luckyDrawPoolData) {
        luckyDrawPoolData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(luckyDrawPoolData.getDrawTimesMap())));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawPoolData.getTodayRealRewardTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawPoolData.getResetTime()));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawPoolData.getActivityId()));
        if (getDb().executeNoneQuery(updateLuckyDrawPoolDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertLuckyDrawPoolDataSql = "insert into `t_n_lucky_draw_pool`(activityId,drawTimesMap,todayRealRewardTimes,resetTime) values (?,?,?,?)";

    @Override
    protected boolean addData(LuckyDrawPoolData luckyDrawPoolData) {
        luckyDrawPoolData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getChannelServerDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawPoolData.getActivityId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(luckyDrawPoolData.getDrawTimesMap())));
        para.put(i++, new DbParameter(Types.INTEGER, luckyDrawPoolData.getTodayRealRewardTimes()));
        para.put(i++, new DbParameter(Types.BIGINT, luckyDrawPoolData.getResetTime()));
        if (getDb().executeNoneQuery(insertLuckyDrawPoolDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected LuckyDrawPoolData parseData(ResultSet rs) throws SQLException {
        LuckyDrawPoolData luckyDrawPoolData = new LuckyDrawPoolData();
        luckyDrawPoolData.setActivityId(rs.getInt("activityId"));
        luckyDrawPoolData.setDrawTimesMap(JSON.parseObject(rs.getString("drawTimesMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
        luckyDrawPoolData.setTodayRealRewardTimes(rs.getInt("todayRealRewardTimes"));
        luckyDrawPoolData.setResetTime(rs.getLong("resetTime"));
        return luckyDrawPoolData;
    }

}
